package com.zxb.core.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;

import java.security.KeyPair;

/**
 * @author Zxb
 */
public class RsaUtil {

    private static final String PUB_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC8YfiQd2Y0XgS9sdDS6nRJJLkSy2ryURe3QHBmvPmuM17CXlHI3KO7mDHgo2qnJBW5amXZxJxf1y0pSTBWS6n+A2A/MDnsGFW3RYGtEX1VmNXTqgQgErNAaZan4BFQLw+yBRB23ULXO1zeesL5izJkTr9xFA54KBG5JyC/xk37IQIDAQAB";
    private static final String PRI_KEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALxh+JB3ZjReBL2x0NLqdEkkuRLLavJRF7dAcGa8+a4zXsJeUcjco7uYMeCjaqckFblqZdnEnF/XLSlJMFZLqf4DYD8wOewYVbdFga0RfVWY1dOqBCASs0BplqfgEVAvD7IFEHbdQtc7XN56wvmLMmROv3EUDngoEbknIL/GTfshAgMBAAECgYAfMM16BfrrIsIAMofT1DPqxGTMQtfzRQ/7LHUv3Ywtlp3S6tD5w+ZsMhfYDNak0+mw6HfypYYKrFLy6gZR03dSqa6ronmZRHQKhIL2O1t5/Dvl759AqTxwbir7BDP/UNbHXHaulPxyjTQDZ7RkEouSEGjgHMWRf/dS2XBM4qTIkQJBAOSmL6ER1Xda8RMsB4Gz505g/OLN5xsy+lXl+erqRLGvxRMx+3a3olw5tUrO3Xv0WmA74wZopOCty05eFcpJuz0CQQDS6rlljtC5P7Af8eGGl7zEAbReStvuTDaiG24o492JKysg3Sjn+G0Zrhrrrqi97ZkpNoOEEKUSkkTKOVlk/I21AkEA3XyODryzx5Z0iFnSO7YFjkstLJbAE/zhumu4F/Mu7+CQSvPT30ArKqNMFlck/XnKQgZA+GhNrscrdZJ2ESozEQJBAJJQIFJyUxrfMnfGnNN4eYtjP1NWDOFvX0rvSmj1QZ5BIwcGRLU+4bg5zhUGsaono+MyHOPFhC2w6CV8ySXECS0CQDE4LIwa7ydiliZf+Od0EH18nMzzLrXUMu2WHI1/GrDmOtSsQIVRCZAhPTqWlyVYV0yzofvJqkbTHnhMD8uQGys=";

    public static void generateKey(){
        KeyPair pair = SecureUtil.generateKeyPair("RSA");
        String privateKey = Base64.encode(pair.getPrivate().getEncoded());
        System.out.println("私钥\t" + privateKey);
        String publicKey = Base64.encode(pair.getPublic().getEncoded());
        System.out.println("公钥\t" + publicKey);
    }

    public static String encrypt(String text){
        RSA rsa = new RSA(PRI_KEY, PUB_KEY);
        // 私钥加密
        return rsa.encryptBase64(text, KeyType.PrivateKey);
    }

    public static String decrypt(String encryptByPublic){
        RSA rsa = new RSA(PRI_KEY, PUB_KEY);
        // 公钥解密
        return rsa.decryptStr(encryptByPublic, KeyType.PublicKey);
    }

    public static String decryptByPri(String encryptByPublic){
        RSA rsa = new RSA(PRI_KEY, PUB_KEY);
        // 公钥解密
        return rsa.decryptStr(encryptByPublic, KeyType.PrivateKey);
    }

    public static void main(String[] args){
        String xxx = "ayj1wNGr5kPw+wJSDpQVyYEcihOx5yKXUkbL6sVpxy9oxcMfZ4EKsFiDE4AqbkMz34027C2E9IV47JlSs8nOmqAchml/kyG2dnYvitG33bJgApW6KRGShwxRMIAMJ4puVSB3rbJ+w0gp3O4cg3hp6HWWw0dFRr0IDxAIGyQiEn8=";
        String yyy = RsaUtil.decryptByPri(xxx);
        System.out.println(yyy);
        //测试生成
        RsaUtil.generateKey();
        //测试加解密
        String text = "Ab1230bq189";
        String encryptByPublic = RsaUtil.encrypt(text);
        System.out.println(encryptByPublic);
        String decryptByPrivate = RsaUtil.decrypt(encryptByPublic);
        System.out.println(decryptByPrivate);


        String str = "Rt9bCMs8a+HqvckUVef+LvpEdQSc3XZCdB32PgWnlCoukCw9Cel9JZQflbPZW2ZBGqtUjYcSHG98jyVfCZdjpb1FHKMTMVNLNsAhMjp+1ym83m2w4znHxNPPy4F+OrZeOxA2Yn+wN2Br5CCrFomO7m2ZFGH6kT4FvLv9mpPq6Ps=";

        String str2 = RsaUtil.decrypt(str);

        String key = "gxvwq4logxvwq4logxvwq4lo";
        String str3 = DesUtil.decrypt("4a82e7b4b695da113662591cf208499f66eb064505f54173b417c9e00f2ab36b3d8413e35a894961bbe451f440bc5b90",key);
        System.out.println(str2);
        System.out.println(str3);

    }
}
